﻿namespace Draco.DataStructure
{
    public class D_LinkQueue<T>
    {
        private D_Node<T> front;
        private D_Node<T> rear;
        public int Count { get; private set; }

        public D_LinkQueue()
        {
            front = null;
            rear = null;
            Count = 0;
        }

        public bool IsEmpty()
        {
            return Count == 0;
        }

        public void Clear()
        {
            front = null;
            rear = null;
            Count = 0;
        }

        public void Enqueue(T item)
        {
            D_Node<T> newNode = new D_Node<T>(item);

            if (Count == 0)
            {
                front = newNode;
                rear = newNode;
                Count = 1;
            }
            else
            {
                rear.Next = newNode;
                rear = newNode;
                Count++;
            }
        }

        public T Dequeue()
        {
            if (Count == 0)
            {
                //队列为空
                return default(T);
            }
            else if (Count == 1)
            {
                T temp = front.Data;
                front = rear = null;
                Count = 0;
                return temp;
            }
            else
            {
                T temp = front.Data;
                front = front.Next;
                Count--;
                return temp;
            }
        }

        public T Peek()
        {
            if (front != null)
            {
                return front.Data;
            }
            else
            {
                return default(T);
            }
        }
    }
}
